// pages/user/pickup/index.js
const app = getApp()
const api  = require('../../../api/shop')
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
import Dialog from '../../../miniprogram/miniprogram_npm/vant-weapp/dialog/dialog'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: app.getUser().id,
    activeName: 0,
    currentPage: 1,
    perPage: 8,
    total: 0,
    checked: false,
    items: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onPage(true);
  },

  bindItemDetail(e) { 

   this.onItemDetail(e.currentTarget.dataset.index)
  },

  onItemDetail(index) {
     console.log(index)
     const active = this.data.items[index]
    if (active.detail.show) {
        this.setData({
            [`items[${index}].detail.show`]: false
        })
        //active.detail.show = false;
        return;
    }

    if (!active.detail.loaded) {
        app.requestUrl({
            url: api.SHOP_PICKUP_GOODS, 
            data: {listId: active.listId, shopId: this.data.shopId}}).then(res => {
            let {productMergeList, memberMergeList} = res.data.data;
           
            this.setData({
                [`items[${index}].detail.productMergeList`]: productMergeList,
                [`items[${index}].detail.memberMergeList`]: memberMergeList,
                [`items[${index}].detail.show`]: true,
                [`items[${index}].detail.loaded`]: true
            })
            //active.detail.show = true;
        });
    } else {
        // active.detail.show = true;
        this.setData({
            [`items[${index}].detail.show`]: true
        })
    }
},


onPage(init) {
    app.requestUrl({
        url: api.SHOP_PICKUP, 
        data: {
        shopId: this.data.shopId, 
        limit: this.data.perPage, 
        offset: this.data.perPage * (this.data.currentPage - 1)
    }}).then(res => {
        let {rows=[], total} = res.data.data;
        //this.total = total;
        rows.forEach(item => {
            item.key = item.listId;//要更新key，自组件才会更新
            item.detail = {
                show: false,
                loaded: false,
            }
        })

        this.setData({
            total,
            items: this.data.items.concat(rows)
        })
        if (total > 0) {
            this.onItemDetail(0);
            if (total >= this.data.items.length) {
                if (!init) {
                    this.setData({currentPage: ++this.data.currentPage})
                }
            } else {
                Toast({
                    message: "没有更多了~"
                });
            }
           
        }
    });
},

onPickedInput(e) {
    const itemindex = e.currentTarget.dataset.itemindex
    const index = e.currentTarget.dataset.index
    const checked = e.detail
    const item = this.data.items[itemindex]
    const member = item.detail.memberMergeList[index]
    if (member.picked) {
       // this.$toast.fail('不能撤销提货');
        Toast.fail('不能撤销提货')
        return;
    }

    Dialog.confirm({
        title: '提醒',
        message: '是否确认提货，确认后不可撤销？'
    }).then(() => {
        app.requestUrl({
            url: api.SHOP_PICKUP_CONFIRM, 
            data: {
            listId: item.listId, 
            shopId: this.data.shopId, 
            memberId: member.mid
        }}).then(res => {
            // member.picked = checked;
            //this.items[0].key = Date.now();
            this.setData({
                [`items[0].key`]: Date.now(), 
                [`items[${itemindex}].detail.memberMergeList[${index}].picked`]: checked
            })
        });
    });        
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.onPage(false)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})